package com.example.recycler.jdscrollbar;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.leolin.shortcutbadger.ShortcutBadger;

public class MainActivity extends AppCompatActivity {
    private TextView mTv2;
    private TextView mTv1;
    private TextView mTextViewNow;
    private int datapos=0;
    private int mHeight;
    private ObjectAnimator mObjectAnimator1,mObjectAnimator2;
    private List<String> mStrings=new ArrayList<>();
    private int point=0;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    ContentValues cv = new ContentValues();
                    cv.put("badgecount", point++);//更新数
                    getContentResolver().update(Uri.parse("content://com.sec.badge/apps"),
                            cv, "package=?", new String[] { getPackageName() });
                    mHandler.sendEmptyMessageDelayed(1,3000);
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTv2 = (TextView) findViewById(R.id.tv_2);
        mTv1 = (TextView) findViewById(R.id.tv_1);
        int badgeCount = 1;
        ShortcutBadger.applyCount(this, badgeCount); //for 1.1.4+
        for (int i = 'A'; i <= 'Z'; i++) {
            mStrings.add("数据" +(char) i);
        }
        mTextViewNow=mTv1;
        mTv1.setText(mStrings.get(datapos++));
        mTv2.setText(mStrings.get(datapos++));
        mTv1.post(new Runnable() {
            @Override
            public void run() {
                mHeight=mTv1.getHeight();
                setAnim();
            }
        });
    }

    private void setAnim() {
        if (mTextViewNow==mTv1) {
            mObjectAnimator1=ObjectAnimator.ofFloat(mTv1,"translationY",0,-mHeight);
            mObjectAnimator1.setDuration(2000);
            mObjectAnimator2=ObjectAnimator.ofFloat(mTv2,"translationY",mHeight,0);
            mObjectAnimator2.setDuration(2000);
        }else{
            mObjectAnimator2=ObjectAnimator.ofFloat(mTv2,"translationY",0,-mHeight);
            mObjectAnimator2.setDuration(2000);
            mObjectAnimator1=ObjectAnimator.ofFloat(mTv1,"translationY",mHeight,0);
            mObjectAnimator1.setDuration(2000);
        }
        mObjectAnimator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mTextViewNow.setText(mStrings.get(datapos++));
                mTextViewNow=mTextViewNow==mTv1?mTv2:mTv1;
                setAnim();
            }
        });
    }

    public void go1(View v) {
        Toast.makeText(this, "按钮1", Toast.LENGTH_SHORT).show();
        mObjectAnimator1.start();
        mObjectAnimator2.start();
    }

    public void go2(View v) {
        Toast.makeText(this, "按钮2", Toast.LENGTH_SHORT).show();
        mHandler.sendEmptyMessageDelayed(1,3000);
    }

    public void go3(View v) {


    }

    public void go4(View v) {


    }

}
